/*
 * Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
 * trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with trinea.cn.
 */
package m.jclecai.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.activity.WebHelperActivity;
import m.jclecai.com.bean.AdvertBean;


public class BannerAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mInflater;
    private List<AdvertBean> adList;
    private int size;
    private boolean isInfiniteLoop;

    public BannerAdapter(Context context, List<AdvertBean> adList) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.adList = adList;
        this.size = adList.size();
    }

    @Override
    public int getCount() {
        return isInfiniteLoop ? Integer.MAX_VALUE : adList.size();
    }

    /**
     * get really position
     *
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return isInfiniteLoop ? position % size : position;
    }

    @Override
    public View getView(final int position, View view, final ViewGroup container) {
        final ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.image, null);
            holder = new ViewHolder(view);
            holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //String img = adList.get(position).getImage();
        final AdvertBean advertBean = adList.get(getPosition(position));
        String img = advertBean.getImage();
        Glide.with(context).load(img).asBitmap().fitCenter()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)//磁盘缓存
                .skipMemoryCache(false).into(holder.image);//内存缓存

        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(WebHelperActivity.getInstance(context,advertBean.getUrl(),false,advertBean.getName()));
            }
        });

        return view;
    }


    /**
     * @return the isInfiniteLoop
     */
    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /*
     */
    public BannerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    public class ViewHolder {
        ImageView image;

        public ViewHolder(View view) {
            image = ((ImageView) view.findViewById(R.id.image));
        }
    }
}
